package com.java3_9;

public class MyAccount {
    private String id;
    private int balance;

    public MyAccount(){

    }

    //构造器用来初始化
    public MyAccount(String id, int balance) {
        this.id = id;
        this.balance = balance;
    }

    public String getId() {
        return id;
    }


    public int getBalance() {
        return balance;
    }

    @Override
    public String toString() {
        return "账户；" + id + "余额；" + balance;
    }

    public void save(int money) {
        synchronized (this) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            this.notify();
            balance += money;
            System.out.println(Thread.currentThread().getName()+"   "+toString());
        }
    }

    public void withdraw(int money) {
        synchronized (this){
            if(balance>=money){
                balance-=money;
                System.out.println(Thread.currentThread().getName()+"   "+toString());
            }else {
                System.out.println("余额不足！！！");
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
